1 基于R的网络分析

  • Visualizing netowrks with Powerpoint.
  • Introducing igraph pacage with R.
  • Using the Gephi.

2 Do not Forget Powerpoint

3 R package: igraph

  • igraph is a collection of network analysis tools with the emphasis on efficiency, portability and ease of use. igraph is open source and free. igraph can be programmed in R, Python and C/C++.

  • igraph has three basic functionalities.
    • Generating networks
    • Visualizing networks
    • Mining networks

4 Generating Networks

library(igraph)
g1 <- graph.empty()
g2 <- graph( c(1,2,2,3,3,4,5,6), directed=TRUE )
g3 <- graph.star(10, mode="out")
g4 <- graph.lattice(c(5,5))
g5 <- graph.lattice(length=5, dim=2)
g6 <- graph.ring(10)
g7 <- graph.tree(10, 2)
g8 <- graph.full(5, loops=TRUE)
g9 <- graph.full.citation(10)
g10 <- graph.atlas(sample(0:1252, 1))
el <- matrix( c("foo", "bar", "bar", "foobar"), nc=2, byrow=TRUE)
g11 <- graph.edgelist(el)
g12 <- graph.extended.chordal.ring(15, matrix(c(3,12,4,7,8,11), nr=2))

5 Visualization

  • plot(): plot does simple non-interactive 2D plotting to R devices.

  • tkplot(): does interactive 2D plotting using the tcltk package. It can only handle graphs of moderate size, a thousand vertices is probably already too many.

  • rglplot(): is an experimental function to draw graphs in 3D using OpenGL.

g2 <- graph( c(1,2,2,3,3,4,5,6), directed=TRUE )
plot(g2)

g3 <- graph.star(10, mode="out")
plot(g3)

g5 <- graph.lattice(length=5, dim=2)
plot(g5)

g6 <- graph.ring(10)
plot(g6)

g7 <- graph.tree(10, 2)
plot(g7)

g8 <- graph.full(5, loops=TRUE)
plot(g8)

g12 <- graph.extended.chordal.ring(15, matrix(c(3,12,4,7,8,11), nr=2))
plot(g12)

test <- read.csv('block4.csv', 
                 head = FALSE, stringsAsFactors = FALSE)
g <- graph.data.frame(test,directed = FALSE)
plot(g,vertex.size=5,layout=layout.fruchterman.reingold,vertex.shape='circle', vertex.label.cex=1.0, vertex.label.color='black', vertex.label=NA) 

#classic random graphs
g13 <- erdos.renyi.game(100,2/100,type='gnp')
plot(g13,layout=layout.fruchterman.reingold,
     vertex.size=5,vertex.label=NA)

#preferential attachment and variations
g14 <- barabasi.game(100)
plot(g14,layout=layout.fruchterman.reingold,
     vertex.size=5,vertex.label=NA,edge.arrow.size=0.1)

6 Mining Graph

  • Computing features of graphs

  • Community Detection

  • Link Prediction

g <- barabasi.game(30)
degree(g)
>  [1] 13  2  8  1  3  2  1  2  1  1  2  1  1  1  2  1  2  1  1  1  2  1  1
> [24]  1  1  1  1  1  1  1
E(g)
> + 29/29 edges from c3e83dc:
>  [1]  2-> 1  3-> 1  4-> 3  5-> 1  6-> 3  7-> 6  8-> 5  9-> 1 10-> 2 11-> 1
> [11] 12-> 1 13-> 1 14-> 1 15-> 1 16-> 5 17-> 3 18->17 19-> 1 20-> 1 21-> 1
> [21] 22-> 3 23->15 24-> 8 25->11 26-> 3 27-> 1 28->21 29-> 3 30-> 3
V(g)
> + 30/30 vertices, from c3e83dc:
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
> [24] 24 25 26 27 28 29 30
shortest.paths(g, v = 1)
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
> [1,]    0    1    1    2    1    2    3    2    1     2     1     1     1
>      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
> [1,]     1     1     2     2     3     1     1     1     2     2     3
>      [,25] [,26] [,27] [,28] [,29] [,30]
> [1,]     2     2     1     2     2     2
  • Centrality: closeness(), betweenness() and page.rank()

  • Community Detection: walktrap.community(), spinglass.community() and egde.betweenness.community()

  • Others

karate <- make_graph("Zachary")
wc <- cluster_walktrap(karate)
modularity(wc)
## [1] 0.3532216
membership(wc)
##  [1] 1 1 2 1 5 5 5 1 2 2 5 1 1 2 3 3 5 1 3 1 3 1 3 4 4 4 3 4 2 3 2 2 3 3
plot(wc, karate)

  • Community Detection for Clustered Attributed Graphs via a Variational EM Algorithm. The 3rd ASE Conference series on Big Data Science and Computing, August 4-7, 2014.